﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <meta charset="UTF-8">
    <title>商户报表</title>
    <link rel="stylesheet" type="text/css" href="~/Content/jquery-easyui-1.4.2/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="~/Content/jquery-easyui-1.4.2/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="~/Content/jquery-easyui-1.4.2/demo.css">
    <link href="~/Content/Styles/Order.css" rel="stylesheet" />
    <script type="text/javascript" src="~/Content/jquery-easyui-1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="~/Content/jquery-easyui-1.4.2/jquery.easyui.min.js"></script>
    <script src="~/Content/jquery-easyui-1.4.2/locale/easyui-lang-zh_CN.js"></script>
    <style>
        .dataTable {
            border-collapse: collapse;
            border: 1px solid #666;
            /*border-right: 1px solid #666;
            border-bottom: 1px solid #666;*/
        }

            .dataTable td {
                border: 1px solid #666;
                /*border-left: 1px solid #666;
                border-top: 1px solid #666;*/
            }

        .trCenter td {
            text-align: center;
        }
    </style>
    <script>
        $(function () {
            $("#TJDiv").hide();
            $.get("/MerReport/getMer", {}, function (database) {
                $('#WaltDep').combobox({
                    valueField: 'MerID',
                    textField: 'MerName',
                    data: database
                });
            });
            $('#customer').combobox({
                valueField: 'id',
                textField: 'text',
                data: [{
                    id: '',
                    text: '全部'
                }, {
                    id: 'user',
                    text: '一般用户'
                }, {
                    id: 'org',
                    text: '单位用户'
                }],
                //onSelect: function (val) {
                //    alert(val.id);
                //}
            });
            dateboxSet();
        });

        //生成营业统计表
        function Search(bs) {
            var merid = $('#WaltDep').combobox('getValue');//商户ID
            var customer = $('#customer').combobox('getValue');//商户ID
            var merText = $('#WaltDep').combobox('getText');//商户名称
            //console.log(merText);
            var sTime = $('#WaltFlowStime').textbox('getValue').replace("-", "").replace("-", "");//开始日期
            var eTime = $('#WaltFlowEtime').textbox('getValue').replace("-", "").replace("-", "");//结束日期

            if (sTime != "" || eTime != "") {//条件不能为空merid != "" || 
                if (sTime > eTime) {//开始日期不能大于结束日期
                    $.messager.alert("友情提示", "开始日期不能大于结束日期");
                } else {
                    var postdata = {
                        MerID: merid,//商户ID
                        Customer: customer,//消费对象
                        sTime: sTime,//开始日期
                        eTime: eTime//结束日期
                    };
                    //发送异步请求到后台保存用户数据
                    $.post("/MerReport/SearchReportTJ", postdata, function (data) {
                        console.log(data);
                        if (data) {
                            //显示表格块
                            $("#TJDiv").show();
                            //删除表格中所有行
                            var tb = document.getElementById('TJTable');
                            var rowNum = tb.rows.length;
                            for (i = 0; i < rowNum; i++) {
                                tb.deleteRow(i);
                                rowNum = rowNum - 1;
                                i = i - 1;
                            }
                            var nametype = "";
                            var lstype = "";
                            if (merid != null & merid != "") {
                                nametype = "营业统计表";
                                lstype = "营业流水账";

                            }
                            else {
                                nametype = "总商户营业统计表";
                                lstype = "总商户营业流水账";

                            }
                            var namemer = "";
                            if (bs == "yyb") {//营业表
                                $('#TJTable').append("<tr><td colspan='5' style='text-align: center; font-size: 20px;'>" + nametype + "</td></tr>");
                                var id;
                                for (var item in data.list_Mer) { 
                                    //alert(data.list_Mer[item].HowCount);
                                    var X = data.list_Mer[item].MerName;
                                    id = data.list_Mer[item].MerID;
                                    var sk = data.list_Mer[item].HowCount;
                                    var num = data.list_Mer[item].DelFormMon * 0.01;
                                   $('#TJTable').append("<tr><td colspan='2'>时间：" + $('#WaltFlowStime').textbox('getValue') + "至" + $('#WaltFlowEtime').textbox('getValue') + "</td><td colspan='3'>餐厅：" + data.list_Mer[item].MerName + "</td></tr>");
                                   $('#TJTable').append("<tr class='trCenter'><td style='width: 40%'>部门（分类）</td><td style='width: 15%'>发生金额</td><td style='width: 15%'>折扣金额</td><td style='width: 15%'>包装费</td><td style='width: 15%'>实际金额</td></tr>");
                                    for (var i in data.rows) {
                                        var sti;
                                        var item = data.rows[i].nej;
                                        for(var g in item){
                                            sti = item[g];
                                            break;
                                        }
                                        //alert(sti.DishesR.MerID + "+" +id );
                                        if (sti.DishesR.MerID == id) {
                                    $('#TJTable').append("<tr class='trCenter'><td>" + data.rows[i].typeName + "</td><td>" + getFloatStr(data.rows[i].fsje * 0.01) + "</td><td>" + getFloatStr(data.rows[i].zkje * 0.01) + "</td><td>" + data.rows[i].bzf+ "</td><td>" + getFloatStr(data.rows[i].sjje * 0.01) + "</td></tr>" +
                                    "<tr class='trCenter'><td></td><td colspan='4'>合计：" + getFloatStr(data.rows[i].sjje * 0.01 + data.rows[i].bzf) + "</td></tr>");

                                        }
                                    }
                                    if (sk!=1&sk!=null&sk!=0) {
                                        $('#TJTable').append("<tr class='trCenter' id='show2'><td colspan='5' style='text-align: center'>" + X + "商户合计：" + num + "</td></tr>");

                                    }
                                    //$('#TJTable').append("<tr class='trCenter'><td></td><td colspan='4'>商户合计：" +data.list_Mer[item].DelFormMon + "</td></tr>");

                                }  
                                $('#TJTable').append("<tr><td colspan='5' style='text-align: center'>总计：" + getFloatStr((data.total + data.totalMon) * 0.01) + "</td></tr>");
                            } else if (bs = "lsz") {//流水账
                                $('#TJTable').append("<tr><td colspan='5' style='text-align: center; font-size: 20px;'>" + lstype + "</td></tr>");
                                var id;
                                for (var item in data.list_Mer) { 
                                    var sk = data.list_Mer[item].HowCount;
                                    var X = data.list_Mer[item].MerName;
                                    //alert(data.list_Mer[item].MerID);
                                    id = data.list_Mer[item].MerID;
                                    var num = data.list_Mer[item].DelFormMon * 0.01;
                                    $('#TJTable').append("<tr><td colspan='2'>时间：" + $('#WaltFlowStime').textbox('getValue') + "至" + $('#WaltFlowEtime').textbox('getValue') + "</td><td colspan='3'>餐厅：" + data.list_Mer[item].MerName + "</td></tr>");
                                    for (var i in data.rows) {
                                        var sti;
                                        var item = data.rows[i].nej;
                                        for (var g in item) {
                                            sti = item[g];
                                            break;
                                        }
                                        if (sti.DishesR.MerID == id) { 
                                        $('#TJTable').append("<tr><td colspan='5'>部门（分类）：" + data.rows[i].typeName + "</td></tr>" +
                                            "<tr class='trCenter'><td style='width: 40%'>菜名</td><td style='width: 15%'>实价</td><td style='width: 15%'>数量</td><td style='width: 15%'>包装费</td><td style='width: 15%'>金额</td></tr>");
                                         //console.log(item);
                                        for (var a in item) {
                                            $('#TJTable').append("<tr class='trCenter'><td>" + item[a].DishesR.DishesNmae + "</td><td>" + getFloatStr(item[a].DishesR.DishesPay * 0.01) + "</td><td>" + item[a].DishesR.DishesCount + "</td><td>" + item[a].ST + "</td><td>" + getFloatStr(item[a].DishesR.DishesActual * 0.01) + "</td></tr>");
                                        }
                                        $('#TJTable').append("<tr class='trCenter'><td colspan='2'>合计</td><td>" + data.rows[i].count + "</td><td>" + data.rows[i].bzf + "</td><td>" + getFloatStr(data.rows[i].sjje * 0.01) + "</td></tr>");
                                        }
                                        //$('#TJTable').append("<tr class='trCenter'><td colspan='4'>合计</td><td>" + data.rows[i].count + "</td><td>" + data.rows[i].DelPackMon + "</td><td>" + getFloatStr(data.rows[i].sjje * 0.01) + "</td></tr><tr><td colspan='4'>&nbsp;</td></tr>");
                                    }
                                    if (sk != 1 & sk != null & sk != 0) {
                                        $('#TJTable').append("<tr id='show2'><td colspan='5' style='text-align: center'>" + X + "商户合计：" + num + "</td></tr>");
                                    }
                                }
                                $('#TJTable').append("<tr><td colspan='5' style='text-align: center'>总计：" + getFloatStr((data.total + data.totalMon) * 0.01) + "</td></tr>");
                            }
                        }
                        else {
                            $.messager.alert("友情提示", "查询失败");
                        }
                    });
                }
            } else {
                $.messager.alert("友情提示", "时间条件不能为空");
            }
        };
        //将传入数据转换为字符串,并清除字符串中非数字与.的字符  
        //按数字格式补全字符串  
        var getFloatStr = function (num) {
            num += '';
            num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符  

            if (/^0+/) //清除字符串开头的0  
                num = num.replace(/^0+/, '');
            if (!/\./.test(num)) //为整数字符串在末尾添加.00  
                num += '.00';
            if (/^\./.test(num)) //字符以.开头时,在开头添加0  
                num = '0' + num;
            num += '00';        //在字符串末尾补零  
            num = num.match(/\d+\.\d{2}/)[0];
            return num;
        };
        //打印
        function myPrint(obj) {
            var newWindow = window.open("打印窗口", "_blank");//打印窗口要换成页面的url
            var docStr = obj.innerHTML;
            newWindow.document.write(docStr);
            newWindow.document.close();
            newWindow.print();
            newWindow.close();
        }
        function preview() {
            bdhtml = window.document.body.innerHTML;
            sprnstr = "<!--startprint-->";
            eprnstr = "<!--endprint-->";
            prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
            prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
            window.document.body.innerHTML = prnhtml;
            window.print();
        }

        //设置日历表的选择范围
        function dateboxSet() {
            // 这是只允许选择今后10天之内的日期(修改后只能选择今天之前的时间)
            $('#WaltFlowStime').datebox('calendar').calendar({
                validator: function (date) {
                    var now = new Date();
                    var d1 = new Date(now.getFullYear(), now.getMonth(), now.getDate());
                    //var d2 = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 10);
                    return d1 >= date;
                    //return d1 <= date && date <= d2;
                }
            });
            $('#WaltFlowEtime').datebox('calendar').calendar({
                validator: function (date) {
                    var now = new Date();
                    var d1 = new Date(now.getFullYear(), now.getMonth(), now.getDate());
                    return d1 >= date;
                }
            });
        }
    </script>
</head>
<body style="padding: 0;">
    <div style="text-align: center; margin: 20px 0">
        <div style="margin-bottom: 10px">
            商户：&nbsp;
        <input id="WaltDep" class="easyui-combobox" style="width: 120px;">&nbsp;消费对象：&nbsp;
            <input id="customer" class="easyui-combobox" style="width: 120px;" @*data-options="
            valueField: 'id',    
            textField: 'text', 
            data:[{    
                id:'',    
                text:'全部'   
            },{    
                id:'user',    
                text:'一般用户'   
            },{    
                id:'org',    
                text:'单位用户'   
            }],
                onSelect:function (val){
                alert(val.id);
            }"*@
            >
            @*<select id="customer" class="easyui-combobox">
                <option value="" selected>全部</option>
                <option value="user">一般用户</option>
                <option value="org">单位用户</option>
            </select>*@
            &nbsp;开始时间:&nbsp;
        <input id="WaltFlowStime" class="easyui-datebox" style="width: 120px">&nbsp;
        到：
        <input id="WaltFlowEtime" class="easyui-datebox" style="width: 120px">
        </div>
        <div>
            <a href="#" class="easyui-linkbutton" onclick="Search('yyb')" iconcls="icon-search">查询营业表</a>
            <a href="#" class="easyui-linkbutton" onclick="Search('lsz')" iconcls="icon-search">查询流水账</a>
        </div>
    </div>
    <div id="TJDiv" style="margin-top: 50px;">
        <!--startprint-->
        <table id="TJTable" class="dataTable" style="width: 90%; margin: 0 auto; margin-bottom: 30px;">
            <!--<tr>
                        <td colspan="4" style="text-align: center; font-size: 20px;">营业统计表</td>
                    </tr>
                    <tr>
                        <td colspan="2">时间：2016年1月1日-2016年1月31日</td>
                        <td colspan="2">餐厅：西苑餐厅</td>
                    </tr>
                    <tr class="trCenter">
                        <td style="width: 40%">部门（分类）</td>
                        <td style="width: 20%">发生金额</td>
                        <td style="width: 20%">折扣金额</td>
                        <td style="width: 20%">实际金额</td>
                    </tr>
                    <tr class="trCenter">
                        <td>快餐</td>
                        <td>100.00</td>
                        <td>0</td>
                        <td>100.00</td>
                    </tr>
                    <tr class="trCenter">
                        <td></td>
                        <td colspan="3">合计：100.00</td>
                    </tr>
                    <tr class="trCenter">
                        <td>快餐</td>
                        <td>100.00</td>
                        <td>0</td>
                        <td>100.00</td>
                    </tr>
                    <tr class="trCenter">
                        <td></td>
                        <td colspan="3">合计：100.00</td>
                    </tr>
                    <tr>
                        <td colspan="4" style="text-align: center">总计：100.00</td>
                    </tr>-->
        </table>
        <!--endprint-->
        <div style="text-align: center">
            <a href="" class="easyui-linkbutton" iconcls="icon-print" onclick="preview()" @*onclick="myPrint(document.getElementById('TJTable'))"*@>打印</a>
        </div>
    </div>
    <!--<table id="LSTable" class="dataTable" style="width: 90%; margin: 0 auto;">
                    <tr>
                        <td colspan="4" style="text-align: center; font-size: 20px;">营业流水账</td>
                    </tr>
                    <tr>
                        <td colspan="2">时间：2016年1月1日-2016年1月31日</td>
                        <td colspan="2">餐厅：西苑餐厅</td>
                    </tr>
                    <tr>
                        <td colspan="4">部门（分类）：快餐</td>
                    </tr>
                    <tr class="trCenter">
                        <td style="width: 40%">菜名</td>
                        <td style="width: 20%">实价</td>
                        <td style="width: 20%">数量</td>
                        <td style="width: 20%">金额</td>
                    </tr>
                    <tr class="trCenter">
                        <td>叉烧饭</td>
                        <td>10.00</td>
                        <td>56</td>
                        <td>560.00</td>
                    </tr>
                    <tr class="trCenter">
                        <td>叉烧饭</td>
                        <td>10.00</td>
                        <td>22</td>
                        <td>220.00</td>
                    </tr>
                    <tr class="trCenter">
                        <td colspan="2">合计</td>
                        <td>78</td>
                        <td>780.00</td>
                    </tr>
                    <tr>
                        <td colspan="4">部门（分类）：快餐</td>
                    </tr>
                    <tr class="trCenter">
                        <td style="width: 40%">菜名</td>
                        <td style="width: 20%">实价</td>
                        <td style="width: 20%">数量</td>
                        <td style="width: 20%">金额</td>
                    </tr>
                    <tr class="trCenter">
                        <td>叉烧饭</td>
                        <td>10.00</td>
                        <td>56</td>
                        <td>560.00</td>
                    </tr>
                    <tr class="trCenter">
                        <td>叉烧饭</td>
                        <td>10.00</td>
                        <td>22</td>
                        <td>220.00</td>
                    </tr>
                    <tr class="trCenter">
                        <td colspan="2">合计</td>
                        <td>78</td>
                        <td>780.00</td>
                    </tr>
                    <tr>
                        <td colspan="4">&nbsp;</td>
                    </tr>
                    <tr class="trCenter">
                        <td colspan="2">总计</td>
                        <td>156</td>
                        <td>1560.00</td>
                    </tr>
                </table>-->
</body>
</html>
